let boats = [];
var featureType = {
    boat: "boat",//波浪艇,
    AIS: "AIS"//AIS
}

function getBoats(callback) {
    // mockBoats();
    fetch('/boat/boat/all')
        .then(res => res.json())
        .then(res => {
            console.log(res)
            if (res.code === 0) {
                boats = res.data
                sendBoatCodes();
                callback();
            }
        })
}

/**
 * 接收到实时信息
 * @param status
 */
function statusGot(status) {
    // console.log('statusGot', status.boatCode)
    let boat = getBoatByCode(status.boatCode);
    if (boat) {
        boat.statusList.push(status);
        if (boat.statusList.length > 10) boat.statusList.shift();

        if (rightDivShown && rightDivCode == boat.code) { // 如果打开的窗口是当前船
            setStatus(boat)
        }
    }
}


function getBoatByCode(code) {
    code += "";
    let boat = boats.find((b => b.code === code))
    if (!boat) return null;
    return boat
}

function getStatusListFromBoats(){
    var statusList = [];
    for (let i = 0; i < boats.length; i++) {
        var boat = boats[i];
        var status = boat.statusList.slice(-1)[0]
        if (status) statusList.push(status);
    }
    return statusList;
}

function getBoatGeoInfo(statusList){
    var boatGeoInfoArray = [];
    for(let i=0;i<statusList.length;i++){
        var status = statusList[i];
        if(status !== undefined){
            boatGeoInfoArray[i] = {
                "lng": status.lng,
                "lat": status.lat,
                "angle": status.headingUp,
                "code": status.boatCode,
            };
        }
    }
    return boatGeoInfoArray;
}
